(function () {
	if (typeof AJAX_COMPRESS_FN != "function") {
		AJAX_COMPRESS_FN = __compressStr__;
	}

	var getToken = function () {
		var cookies = document.cookie.match('_.user.token' + '=(.*?)(;|$)');
		if (cookies && cookies[1]) return cookies[1];
		return '';
	}

	var parseParams = function (data) {
		return (function () {
			var ret = {},
				seg = data.split('&'),
				len = seg.length,
				i = 0,
				s;
			for (; i < len; i++) {
				if (!seg[i]) {
					continue;
				}
				s = seg[i].split('=');
				ret[s[0]] = decodeURIComponent(s[1], true);
			}
			return ret;
		})()
	}

	var isStatic = function(url){
		var suffixMatch = url.match(/^.+\/\w+(\.\w+)/i),
				binMatch = url.match(/(\/\w+-bin\/)/i),
				isStatic = false;
		if (suffixMatch && suffixMatch[1]) {
			if (['.js', '.css', '.xml', '.html', '.htm', '.txt', '.json'].indexOf(suffixMatch[1]) != -1 && !binMatch) isStatic = true;
		}
		return isStatic;
	}

	var XJSON_MODE = typeof AJAX_WITH_XJSON != "undefined" && AJAX_WITH_XJSON;

	if (XJSON_MODE) {	//这里注释掉，在非加密状态需要自动追加token，20210205 by shijin
		if (typeof jQuery != "undefined" && jQuery.ajaxSetup) {
			jQuery.ajaxSetup({
				beforeSend: function (xhr, eOpts) {
					var _isStatic = isStatic(eOpts.url),
							_jsonMode = eOpts.contentType.indexOf('application/json') != -1;

					if (_jsonMode){
						if (typeof eOpts.data == "string"){
							try{
								eOpts.data = jQuery.param(JSON.parse(eOpts.data))
							}catch(e){}
						}
					}

					if (!_isStatic) {
						var _data = parseParams(eOpts.data || "");
						//if (getToken() != '') _data.userToken = getToken();
						if (XJSON_MODE) _data.__sec_io_sequence__ = new Date().getTime();
						if (_jsonMode && !XJSON_MODE){
							eOpts.data = JSON.stringify(_data);
						}else{
							eOpts.data = jQuery.param(_data);
						}
					}

					if (XJSON_MODE) {
						xhr.setRequestHeader("Content-Type", "application/x-json");
						if (eOpts.data) {
							eOpts.data = AJAX_COMPRESS_FN(JSON.stringify(_data));
						}
					}
				},
			});
		}

		if (typeof Ext != "undefined" && Ext.Ajax) {
			Ext.Ajax.on('beforerequest', function (conn, options, eOpts) {
				var _isStatic = isStatic(options.url);

				if (!_isStatic){
					if (!options.params) options.params = {};
					//if (getToken() != '') options.params.userToken = getToken();
					if (XJSON_MODE) options.params.__sec_io_sequence__ = new Date().getTime();					
				}

				if (XJSON_MODE) {
					if (options.jsonData) {
						conn.setDefaultPostHeader('application/x-json');
						options.params.__jsonData = options.jsonData;
						options.params = AJAX_COMPRESS_FN(JSON.stringify(options.params));
						delete options.jsonData;
					} else {
						conn.setDefaultPostHeader('application/x-form-json');
						options.params = AJAX_COMPRESS_FN(JSON.stringify(options.params));
					}
				}
			})
		}
	}

	if (typeof jQuery != "undefined" && jQuery.ajaxSetup) {
		jQuery.ajaxSetup({
			complete: function (xhr, textStatus) { //监听
				var response = {};
				try {
					response = JSON.parse(xhr.responseText);
				} catch (e) { }
				switch (response.clientAction) {
					case 'logout': 
						if (top.mainframe) {
							top.mainframe.fireEvent('logout', top.mainframe, response.emsg);
						}
						break;
					case 'lock': 
						if (top.mainframe) {
							top.mainframe.fireEvent('lock', top.mainframe, response.emsg);
						}
						break;
				}
			}
		});
	}

	/**
	 * compress string and encode to base64
	 * WuDongsheng @ 2020-05-22
	 * @param {String|Object} str
	 */
	function __compressStr__(str) {
		if (str == null) return "";
		if (typeof str != "string") str = JSON.stringify(str);
		var keyStrBase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_@"; //+/=
		var i, value,
			context_dictionary = {},
			context_dictionaryToCreate = {},
			context_c = "",
			context_wc = "",
			context_w = "",
			context_enlargeIn = 2, // Compensate for the first entry which should not count
			context_dictSize = 3,
			context_numBits = 2,
			context_data = [],
			context_data_val = 0,
			context_data_position = 0,
			ii;

		for (ii = 0; ii < str.length; ii += 1) {
			context_c = str.charAt(ii);
			if (!Object.prototype.hasOwnProperty.call(context_dictionary, context_c)) {
				context_dictionary[context_c] = context_dictSize++;
				context_dictionaryToCreate[context_c] = true;
			}

			context_wc = context_w + context_c;
			if (Object.prototype.hasOwnProperty.call(context_dictionary, context_wc)) {
				context_w = context_wc;
			} else {
				if (Object.prototype.hasOwnProperty.call(context_dictionaryToCreate, context_w)) {
					if (context_w.charCodeAt(0) < 256) {
						for (i = 0; i < context_numBits; i++) {
							context_data_val = (context_data_val << 1);
							if (context_data_position == 5) {
								context_data_position = 0;
								context_data.push(keyStrBase64.charAt(context_data_val));
								context_data_val = 0;
							} else {
								context_data_position++;
							}
						}
						value = context_w.charCodeAt(0);
						for (i = 0; i < 8; i++) {
							context_data_val = (context_data_val << 1) | (value & 1);
							if (context_data_position == 5) {
								context_data_position = 0;
								context_data.push(keyStrBase64.charAt(context_data_val));
								context_data_val = 0;
							} else {
								context_data_position++;
							}
							value = value >> 1;
						}
					} else {
						value = 1;
						for (i = 0; i < context_numBits; i++) {
							context_data_val = (context_data_val << 1) | value;
							if (context_data_position == 5) {
								context_data_position = 0;
								context_data.push(keyStrBase64.charAt(context_data_val));
								context_data_val = 0;
							} else {
								context_data_position++;
							}
							value = 0;
						}
						value = context_w.charCodeAt(0);
						for (i = 0; i < 16; i++) {
							context_data_val = (context_data_val << 1) | (value & 1);
							if (context_data_position == 5) {
								context_data_position = 0;
								context_data.push(keyStrBase64.charAt(context_data_val));
								context_data_val = 0;
							} else {
								context_data_position++;
							}
							value = value >> 1;
						}
					}
					context_enlargeIn--;
					if (context_enlargeIn == 0) {
						context_enlargeIn = Math.pow(2, context_numBits);
						context_numBits++;
					}
					delete context_dictionaryToCreate[context_w];
				} else {
					value = context_dictionary[context_w];
					for (i = 0; i < context_numBits; i++) {
						context_data_val = (context_data_val << 1) | (value & 1);
						if (context_data_position == 5) {
							context_data_position = 0;
							context_data.push(keyStrBase64.charAt(context_data_val));
							context_data_val = 0;
						} else {
							context_data_position++;
						}
						value = value >> 1;
					}


				}
				context_enlargeIn--;
				if (context_enlargeIn == 0) {
					context_enlargeIn = Math.pow(2, context_numBits);
					context_numBits++;
				}
				// Add wc to the dictionary.
				context_dictionary[context_wc] = context_dictSize++;
				context_w = String(context_c);
			}
		}

		// Output the code for w.
		if (context_w !== "") {
			if (Object.prototype.hasOwnProperty.call(context_dictionaryToCreate, context_w)) {
				if (context_w.charCodeAt(0) < 256) {
					for (i = 0; i < context_numBits; i++) {
						context_data_val = (context_data_val << 1);
						if (context_data_position == 5) {
							context_data_position = 0;
							context_data.push(keyStrBase64.charAt(context_data_val));
							context_data_val = 0;
						} else {
							context_data_position++;
						}
					}
					value = context_w.charCodeAt(0);
					for (i = 0; i < 8; i++) {
						context_data_val = (context_data_val << 1) | (value & 1);
						if (context_data_position == 5) {
							context_data_position = 0;
							context_data.push(keyStrBase64.charAt(context_data_val));
							context_data_val = 0;
						} else {
							context_data_position++;
						}
						value = value >> 1;
					}
				} else {
					value = 1;
					for (i = 0; i < context_numBits; i++) {
						context_data_val = (context_data_val << 1) | value;
						if (context_data_position == 5) {
							context_data_position = 0;
							context_data.push(keyStrBase64.charAt(context_data_val));
							context_data_val = 0;
						} else {
							context_data_position++;
						}
						value = 0;
					}
					value = context_w.charCodeAt(0);
					for (i = 0; i < 16; i++) {
						context_data_val = (context_data_val << 1) | (value & 1);
						if (context_data_position == 5) {
							context_data_position = 0;
							context_data.push(keyStrBase64.charAt(context_data_val));
							context_data_val = 0;
						} else {
							context_data_position++;
						}
						value = value >> 1;
					}
				}
				context_enlargeIn--;
				if (context_enlargeIn == 0) {
					context_enlargeIn = Math.pow(2, context_numBits);
					context_numBits++;
				}
				delete context_dictionaryToCreate[context_w];
			} else {
				value = context_dictionary[context_w];
				for (i = 0; i < context_numBits; i++) {
					context_data_val = (context_data_val << 1) | (value & 1);
					if (context_data_position == 5) {
						context_data_position = 0;
						context_data.push(keyStrBase64.charAt(context_data_val));
						context_data_val = 0;
					} else {
						context_data_position++;
					}
					value = value >> 1;
				}


			}
			context_enlargeIn--;
			if (context_enlargeIn == 0) {
				context_enlargeIn = Math.pow(2, context_numBits);
				context_numBits++;
			}
		}

		// Mark the end of the stream
		value = 2;
		for (i = 0; i < context_numBits; i++) {
			context_data_val = (context_data_val << 1) | (value & 1);
			if (context_data_position == 5) {
				context_data_position = 0;
				context_data.push(keyStrBase64.charAt(context_data_val));
				context_data_val = 0;
			} else {
				context_data_position++;
			}
			value = value >> 1;
		}

		// Flush the last char
		while (true) {
			context_data_val = (context_data_val << 1);
			if (context_data_position == 5) {
				context_data.push(keyStrBase64.charAt(context_data_val));
				break;
			} else context_data_position++;
		}

		// Append the padding char
		// for (i = ((4 - (context_data.length % 4)) % 4); i > 0; i--) {
		// 	context_data.push('=');
		// }
		return context_data.join('');
	}

})();
